package com.nbot.server.controller;

import com.nbot.server.annotation.Log;
import com.nbot.server.domain.SysParameterDO;
import com.nbot.server.service.SysParameterService;
import com.nbot.server.util.PageUtils;
import com.nbot.server.util.Query;
import com.nbot.server.util.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统参数
 *
 * @author lovewinner
 * @email nbot@163.com
 * @date 2018-06-28 21:46:07
 */

@Controller
@RequestMapping("/server/sysParameter")
public class SysParameterController {
    @Autowired
    private SysParameterService sysParameterService;

    @GetMapping()
    @RequiresPermissions("server:sysParameter:sysParameter")
    String SysParameter() {
        return "server/sysParameter/sysParameter";
    }

    @ResponseBody
    @Log("查看系统参数")
    @GetMapping("/list")
    @RequiresPermissions("server:sysParameter:sysParameter")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<SysParameterDO> sysParameterList = sysParameterService.list(query);
        int total = sysParameterService.count(query);
        PageUtils pageUtils = new PageUtils(sysParameterList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("server:sysParameter:add")
    String add() {
        return "server/sysParameter/add";
    }

    @GetMapping("/edit/{autoId}")
    @RequiresPermissions("server:sysParameter:edit")
    String edit(@PathVariable("autoId") Long autoId, Model model) {
        SysParameterDO sysParameter = sysParameterService.get(autoId);
        model.addAttribute("sysParameter", sysParameter);
        return "server/sysParameter/edit";
    }

    /**
     * 保存
     */
    @Log("保存系统参数")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("server:sysParameter:add")
    public R save(SysParameterDO sysParameter) {
        if (sysParameterService.save(sysParameter) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("修改系统参数")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("server:sysParameter:edit")
    public R update(SysParameterDO sysParameter) {
        sysParameterService.update(sysParameter);
        return R.ok();
    }

    /**
     * 删除
     */
    @Log("删除系统参数")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("server:sysParameter:remove")
    public R remove(Long autoId) {
        if (sysParameterService.remove(autoId) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @Log("批量删除系统参数")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("server:sysParameter:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] autoIds) {
        sysParameterService.batchRemove(autoIds);
        return R.ok();
    }

}
